package cn.ava.org.controller;

import cn.ava.basic.annotation.PreAuthorize;
import cn.ava.basic.utils.AjaxResult;
import cn.ava.basic.utils.LoginContext;
import cn.ava.basic.utils.PageList;
import cn.ava.org.domain.Department;
import cn.ava.org.query.DepartmentQuery;
import cn.ava.org.service.IDepartmentService;
import cn.ava.user.domain.Logininfo;
import com.sun.net.httpserver.HttpsServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName: DepartmentController
 * @Description: 部门控制层接口
 * @Author: Avavaava
 * @Date: 2022/6/24 15:07
 * @version: 23.53
 *
 */
@Api(value = "部门Controller层",description = "部门列表CRUD模块")
@RestController
@RequestMapping("/dept")
public class DepartmentController {

    // 实例化接口对象
    @Autowired
    private IDepartmentService departmentService;

    /** 
    * @Title: list
    * @Description: 查询所有部门列表
    * @Author: Avavaava
    * @Version: 23.53
    * @Date:  2022/6/24 16:27
    * @param: 
    * @return: 
    **/
    @GetMapping
    @ApiOperation(value = "查询所有部门")
    public List<Department> list() {
        return departmentService.loadAll();
    }
    
    /** 
    * @Title: getById
    * @Description: 根据部门id查询部门
    * @Author: Avavaava
    * @Version: 23.53
    * @Date:  2022/6/24 16:28
    * @param: 
    * @return: 
    **/
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询部门数据")
    @PreAuthorize(name = "根据ID查询部门数据",sn = "department:getById")
    public Department getById(@PathVariable("id") Long id) {
        return departmentService.getById(id);
    }

    /** 
    * @Title: delete
    * @Description: 根据部门id删除部门列表数据
    * @Author: Avavaava
    * @Version: 23.53
    * @Date:  2022/6/24 16:28
    * @param: 
    * @return: 
    **/
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "根据ID删除部门数据",sn = "department:delete")
    public AjaxResult delete(@ApiParam(value = "主键",readOnly = true) @PathVariable("id") Long id) {
        try {
            departmentService.delete(id);
           return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    /**
    * @Title: addOrUpt
    * @Description: 新增或修改部门数据
    * @Author: Avavaava
    * @Version: 23.53
    * @Date:  2022/6/24 16:29
    * @param:
    * @return:
    **/
    @PutMapping
    @PreAuthorize(name = "新增或修改部门数据",sn = "department:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody Department department) {
        try{
            if (department.getId() == null) {
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    /**
    * @Title: queryDept
    * @Description: 高级查询部门列表
    * @Author: Avavaava
    * @Version: 23.53
    * @Date:  2022/6/24 16:29
    * @param:
    * @return:
    **/
    @PostMapping
    @PreAuthorize(name = "高级查询部门数据",sn = "department:queryData")
    public PageList<Department> queryDept(@RequestBody DepartmentQuery departmentQuery, HttpServletRequest request){
        return departmentService.queryData(departmentQuery);
    }

    /**
     * @Title: batchDelete
     * @Description: 批量删除数据
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/6/25 11:19
     * @param:
     * @return:
     */
    @PatchMapping
    @PreAuthorize(name = "批量删除部门数据",sn = "department:batchDelete")
    public AjaxResult batchDelete(@RequestBody Long[] id) {
        try {
            departmentService.batchDelete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    /**
     * @Title: getDeptTree
     * @Description: 查询部门树数据
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/6/27 17:50
     * @param:
     * @return:
     */
    @GetMapping("/deptTree")
    @PreAuthorize(name = "获取部门树数据",sn = "department:DeptTree")
    public List<Department> getDeptTree() {
        return departmentService.getDeptTree();
    }

}
